#!/bin/bash
BASE_HOME=`/usr/bin/dirname "$0"`
FILE_NAME=`basename "$0"`
PROCESS_ID="$$"
#CONF_FILE="${BASE_HOME}/mysql_config.list"
CONF_FILE="/apps/shell/keepalived/mysql_config.list"
##load mysql config file 
while read line; do
    eval $line
done < ${CONF_FILE}

#create logfile path if not exists
LOGFILE_PATH=`echo ${LOGFILE%/*}`
if [ ! -d ${LOGFILE_PATH} ]; then
                mkdir -p  ${LOGFILE_PATH} 
fi

##record scripts running logs
exec >> ${LOGFILE} 2>&1


echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]begin Keepalived(vrrp_scripts) check mysql"
##check mysql login PROTOCOL is socket or tcp
if [ "${MYSQL_PROTOCOL}"x = "socket"x ]; then
                MYSQL_L_CMD="${MYSQL_CMD} -u${MYSQL_USER} -p${MYSQL_PASS} -P${MYSQL_PORT} --protocol=socket -S ${SOCKET} -N -L -s"
elif [ "${MYSQL_PROTOCOL}"x = "tcp"x ]; then
                MYSQL_L_CMD="${MYSQL_CMD} -u${MYSQL_USER} -p${MYSQL_PASS} -P${MYSQL_PORT} --protocol=tcp -h${LOCAL_HOST} -N -L -s"
else
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]MYSQL_PROTOCOL=${MYSQL_PROTOCOL},but MYSQL_PROTOCOL parameter is socket or tcp please config it"
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]end Keepalived(vrrp_scripts) check mysql"
                exit 0
fi
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]MYSQL_PROTOCOL=${MYSQL_PROTOCOL},and MYSQL_L_CMD=${MYSQL_L_CMD}"

##if the write vip in this node and this node is master node
WVIP_COUNT=`ip addr | grep -v grep | grep "${WRITE_VIP}" |wc -l`
if [ ${WVIP_COUNT} -eq 1 ]; then
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]VIP ${WRITE_VIP} exists on local host  ${LOCAL_HOST} and mysql is master !"
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]VIP `ip addr | grep -v grep | grep \"${WRITE_VIP}\"`"
else
    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]VIP ${WRITE_VIP} not exists on local host  ${LOCAL_HOST} and  mysql node is slave!"
fi


##check mysql is alived function
check_mysql_alive()
{
i=0
MYSQL_ALIVED=0
COUNTER_RES=0
MYSQL_ALIVE_CHKLOG="${LOGFILE_PATH}/mysql_alive_`date +%Y%m%d%H%M%S`$RANDOM"
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]nohup ${MYSQL_L_CMD}  -e \"${CHECK_SQL}\" > ${MYSQL_ALIVE_CHKLOG} 2>&1 &"
nohup ${MYSQL_L_CMD}  -e "${CHECK_SQL}" > ${MYSQL_ALIVE_CHKLOG} 2>&1 &
while [ $i -lt ${CHECK_TIME} ]; do
    i=`expr $i + 1`
    sleep 1
    if [ -s ${MYSQL_ALIVE_CHKLOG} ]; then
        COUNTER_RES=`cat ${MYSQL_ALIVE_CHKLOG}|grep "COUNTER" |awk -F':' '{print $2}'`
        if [ "${COUNTER_RES}"x = ""x ]; then
                        COUNTER_RES=0
        fi        
        if [ ${COUNTER_RES} -gt 0 ]; then
            MYSQL_ALIVED=1
            cat ${MYSQL_ALIVE_CHKLOG} 
            rm -f ${MYSQL_ALIVE_CHKLOG}
            break;
        else
            continue;
        fi
    fi
done
}

ic=1
while [ $ic -le ${CHECK_COUNT} ]; do
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]start  ${ic} check mysql is or alived!"
    check_mysql_alive
    if [ ${MYSQL_ALIVED} -eq 1 ]; then
                                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]Keepalived(vrrp_scripts):MySQL Database Server is running and check count is ${ic} !"
                                #echo "$processlist"
        #echo "$innodb_status"
        echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]end Keepalived(vrrp_scripts) check mysql"
        exit 0
        break;
    else
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]Keepalived(vrrp_scripts):MySQL Database Server is not running and check count is ${ic}!"
                if [ ${ic} -eq ${CHECK_COUNT} ]; then
                                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]mysql is not running and started switch keepalived!"
                                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]ps -ef | grep mysqld is `ps -ef | grep mysqld | grep -v grep`"
                                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]end Keepalived(vrrp_scripts) check mysql"
                    exit 1
          fi
    fi
    ic=`expr $ic + 1`
done
